$(document).ready(function() {
	
	$("#UserTypeBox").change(function() {
		if($(this).val() == '1')
		{
			GetTeacherList();
			$("#ClassStudents").css('display', 'none');
			$("#ClassTeacher").css('display', 'inline-block');
			
			ToggleTeacherEnrollRemove();
		}
		else
		{
			GetStudentList();
			$("#ClassTeacher").css('display', 'none');
			$("#ClassStudents").css('display', 'inline-block');
			ToggleStudentEnrollRemove();
		}
	});
	
	$("#CourseBox").change(function() {
		RefreshData();
		
	});
	
	$("#AddUserButton").click(function() {
		var SelectedUserID = $('#UserBox').val();
		var SelectedCourseID = $('#CourseBox').val();
		
		
		$(SelectedUserID).each(function(index, value) {
			EnrollUser(value, SelectedCourseID, index, $(SelectedUserID).length);			
		});
		
		
	});

	$("#RemoveUserButton").click(function() {
		var SelectedUserID = $("#UserTypeBox").val() == '1' ? $('#TeacherBox').val() : $('#EnrollmentBox').val();
		var SelectedCourseID = $('#CourseBox').val();
		
		
		$(SelectedUserID).each(function(index, value) {
			RemoveUser(value, SelectedCourseID, index, $(SelectedUserID).length);			
		});
		
	});

	RefreshData();
	
	$("#ClassTeacher").css('display', 'none');
	$("#ClassStudents").css('display', 'inline-block');

});



function GetStudentList()
{
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Get_all_students', CourseID: $("#CourseBox").val() },
			 dataType: 'json',
			 success: function (data) {
				
				 //console.log(data);
				populate_student_list(data);
			 },
			 error: function (e)
			 {
				console.log(e);
			 }
		});
}

function GetTeacherList()
{
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Get_all_teachers', CourseID: $("#CourseBox").val() },
			 dataType: 'json',
			 success: function (data) {
				
				 //console.log(data);
				 populate_teacher_list(data);
				 
			 },
			 error: function (e)
			 {
				//console.log(e);
			 }
		});

}

function GetClassStudents()
{
	//console.log($("#CourseBox").val());
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Get_students_of_a_course', CourseID: $("#CourseBox").val() },
			 dataType: 'json',
			 success: function (data) {
				 populate_dropdown_options('#EnrollmentBox', data)
				 AfterChange();
			 },
			 error: function (e)
			 {
				//console.log(e);
			 }
		});

}

function GetClassTeacher()
{
	//console.log($("#CourseBox").val());
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Get_teacher_of_a_course', CourseID: $("#CourseBox").val() },
			 dataType: 'json',
			 success: function (data) {
				 populate_dropdown_options('#TeacherBox', data);
				 AfterChange();
			 },
			 error: function (e)
			 {
				console.log(e);
			 }
	});

}

function EnrollUser(SelectedUserID, SelectedCourseID, Index, Size)
{	
	
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Enroll_user_to_a_course', UserID: SelectedUserID.toString(), CourseID: SelectedCourseID.toString() },
			 dataType: 'json',
			 success: function (data) {
				 
				 if(Index == (Size - 1))
				 {
					RefreshData();
						
				 }
			 },
			 error: function (e)
			 {

				if(Index == (Size - 1))
				 {
					RefreshData();
				 }
			 }
	});
}

function RemoveUser(SelectedUserID, SelectedCourseID, Index, Size)
{	
	
	$.ajax({
			 url: "webservices.php",
			 type: "POST",
			 data: { Func: 'Remove_user_from_a_course', UserID: SelectedUserID.toString(), CourseID: SelectedCourseID.toString() },
			 dataType: 'json',
			 success: function (data) {
				 

				 if(Index == (Size - 1))
				 {
					RefreshData();
				 }
				 
			 },
			 error: function (e)
			 {

				if(Index == (Size - 1))
				 {
					RefreshData();
				 }
			 }
	});
}



function populate_dropdown_options(SelectID, JsonData)
{
	var OptionsTagString = "";
	
		$(JsonData).each(function () {
			
			OptionsTagString += '<option value="' + this.id + '">' + this.name + '</option>';
		});
		
	$(SelectID).html(OptionsTagString);
	
}

function populate_student_list(StudentData)
{
	var OptionsTagString = "";
	
		$(StudentData).each(function () {
			var EnrolledClass = this.Enrolled == 1 ? ' class="Enrolled" ' : ' class="NotEnrolled" ';

			
			OptionsTagString += '<option' + EnrolledClass + 'value="' + this.id + '">' + this.name + '</option>';
		});
		
	$("#UserBox").html(OptionsTagString);
	
	$(".Enrolled").attr('disabled', 'disabled');
	
}

function populate_teacher_list(TeacherData)
{
	var OptionsTagString = "";
	
		$(TeacherData).each(function () {
			var EnrolledClass = this.Enrolled == 1 ? ' class="Enrolled" ' : ' class="NotEnrolled" ';

			
			OptionsTagString += '<option' + EnrolledClass + 'value="' + this.id + '">' + this.name + '</option>';
		});
		
	$("#UserBox").html(OptionsTagString);
	
	$(".Enrolled").attr('disabled', 'disabled');
	
}

function RefreshData()
{
	if($("#UserTypeBox").val() == '1')
		GetTeacherList();
	else
		GetStudentList();
			
	GetClassStudents();
	GetClassTeacher();

}

function AfterChange()
{
	if($("#UserTypeBox").val() == '1')
	 {
		ToggleTeacherEnrollRemove();
	 }
	 else
	 {
		ToggleStudentEnrollRemove();
	 }
}

function ToggleTeacherEnrollRemove()
{
	console.log($("#TeacherBox"));
	if($("#TeacherBox").find('option').length == 0)
	{
		$("#AddUserButton").removeAttr('disabled');
		$("#RemoveUserButton").attr('disabled', 'disabled');
	}
	else
	{
		$("#AddUserButton").attr('disabled', 'disabled');
		$("#RemoveUserButton").removeAttr('disabled');	
	}
}

function ToggleStudentEnrollRemove()
{
	
	if($("#EnrollmentBox").find('option').length == 0)
	{
		$("#AddUserButton").removeAttr('disabled');
		$("#RemoveUserButton").attr('disabled', 'disabled');
	}
	else
	{
		$("#RemoveUserButton").removeAttr('disabled');	
		$("#AddUserButton").removeAttr('disabled');
	}
}

function ShowLoadingBar()
{
	

}
